package com.example.carproviderbackstage.service.impl.dl.admin;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.carinterface.service.dl.admin.CustomerService;
import com.example.carinterface.utils.ResultJson;
import com.example.carproviderbackstage.mapper.dl.admin.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.util.*;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ResultJson<String> resultJson;



    @Override
    public ResultJson<List<Map<String, Object>>> findCustomer(Map<String, String> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        ResultJson<List<Map<String, Object>>> resultJson = new ResultJson<>();
        List<Map<String, Object>> list = customerMapper.findCustomer(map);

        resultJson.setSuccess("查询成功", list);
        return resultJson;
    }

    @Override
    public ResultJson<String> addCustomer(Map<String, Object> map) {
        ResultJson<String> resultJson = new ResultJson<>();
        if (customerMapper.addCustomer(map) == 1) {
            resultJson.setSuccess("添加成功");
        } else {
            resultJson.setSuccess("添加失败");
        }

        return resultJson;
    }

    @Override
    public ResultJson<String> updateCustomer(Map<String, Object> map) {
        ResultJson<String> resultJson = new ResultJson<>();
        if (customerMapper.updateCustomer(map) == 1) {
            resultJson.setSuccess("修改成功");
        } else {
            resultJson.setSuccess("修改失败");
        }

        return resultJson;
    }

    @Override
    public ResultJson<String> updateOwnerCard(Map<String, Object> map) {


        ResultJson<String> resultJson = new ResultJson<>();

        if (customerMapper.updateOwnerCard(map)==1){
            resultJson.setSuccess("修改成功");
        }else {
            resultJson.setSuccess("修改失败");
        }

        return resultJson;
    }

    @Override
    public ResultJson<List<Map<String, Object>>> findBrand(Map<String,Object> map) {
        ResultJson<List<Map<String, Object>>> resultJson = new ResultJson<>();
        if (map==null){
            map = new HashMap<>();
        }
        List<Map<String, Object>> brand = customerMapper.findBrand(map);

        for (Map<String, Object> map1 : brand) {
            String typeStr = map1.get("type").toString();
            String idStr = map1.get("id").toString();
            String[] typeArr = typeStr.split(",");
            String[] idArr = idStr.split(",");
            ArrayList<Map<String,Object>> list = new ArrayList<>();
            for (int i = 0; i < idArr.length; i++) {
            Map<String, Object> map2 = new HashMap<>();
                map2.put("id",idArr[i]);
                map2.put("type",typeArr[i]);
            list.add(map2);
            }
            map1.put("id_type",list);
        }

        resultJson.setSuccess("查询成功",brand);
        return resultJson;
    }

    @Override
    public ResultJson<List<Map<String, Object>>> findDriver(Map<String, Object> map) {
        ResultJson<List<Map<String, Object>>> resultJson = new ResultJson<>();
        if (map==null){
            map = new HashMap<>();
        }
        List<Map<String, Object>> driver = customerMapper.findDriver(map);
        resultJson.setSuccess("查询成功", driver);
        return resultJson;
    }

    @Override
    public ResultJson<String> addDriver(Map<String, Object> map) {
        ResultJson<String> resultJson = new ResultJson<>();

        int i = customerMapper.addDriver(map);
        if (i==1){
            resultJson.setSuccess("添加成功");
        }else {
            resultJson.setSuccess("添加失败");
        }
        return resultJson;
    }

    @Override
    public ResultJson<List<Map<String, Object>>> findCarModel(Integer id) {
        ResultJson<List<Map<String, Object>>> r = new ResultJson<>();
        List<Map<String, Object>> list = customerMapper.findCarModel(id);
        r.setSuccess("查询成功",list);
        return r;
    }

    @Override
    public ResultJson<String> updateDriver(Map<String, Object> map) {

      int i =  customerMapper.updateDriver(map);
        if (i==1){
            resultJson.setSuccess("修改成功");
        }else {
            resultJson.setSuccess("修改失败");
        }

        return resultJson;
    }
}
